/** 异步加载标签类 */
export default class Autoload {
  constructor() {
    return Promise.all([
      this.loadExtendResource("./live2d.min.js", "js"),
      this.loadExtendResource("./live2d.music.js", "js"),
      this.loadExtendResource("./live2d.tips.js", "js"),
      this.loadExtendResource("./demo.css", "css"),
      this.loadExtendResource("./Aplayer.min.css", "css"),
      this.loadExtendResource("./APlayer.min.js", "js"),
      this.loadExtendResource("./aplayer.css", "css"),
    ]);
  }
  loadExtendResource(url, type) {
    return new Promise((resolve, reject) => {
      let tag;

      switch (type) {
        case "css":
          tag = document.createElement("link");
          tag.href = url;
          tag.rel = "stylesheet";
          break;
        case "js":
          tag = document.createElement("script");
          tag.src = url;
          break;
      }

      if (tag) {
        tag.onload = () => resolve(url);
        tag.onerror = () => reject(url);
        document.head.appendChild(tag);
      }
    });
  }
}
